package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.annotation.PassToken;
import com.quyeying.entity.TblMerchantEntity;
import com.quyeying.entity.TblPopularizeVideoEntity;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.service.TblMerchantService;
import com.quyeying.service.TblPopularizeVideoService;
import com.quyeying.service.TblProductService;
import com.quyeying.utils.result.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class GetIndexController {

    @Resource
    TblPopularizeVideoService popularizeVideoService;
    @Resource
    TblMerchantService merchantService;

    /**
     * 获取首页信息接口
     */
    @PassToken
    @GetMapping("/getIndexInfo")
    public R getIndexInfo() {
        //最新发布视频
        QueryWrapper<TblPopularizeVideoEntity> popularizeVideoEntityQueryWrapper = new QueryWrapper<>();
        popularizeVideoEntityQueryWrapper.select("id as videoId, video, pid").last("ORDER BY sort desc, create_time desc limit 1");
        Map<String, Object> map = popularizeVideoService.getMap(popularizeVideoEntityQueryWrapper);
        //获取商家列表
        QueryWrapper<TblMerchantEntity> merchantEntityQueryWrapper = new QueryWrapper<>();
        merchantEntityQueryWrapper.select("id, merchant_name as merchantName, merchant_headimg as merchantHeadimg, type").orderByAsc("sort").last("limit 6");
        List<Map<String, Object>> merchantEntityList = merchantService.listMaps(merchantEntityQueryWrapper);
        merchantEntityList.forEach(item ->{
            Long popularity = merchantService.getPopularity(Long.parseLong(item.get("id").toString()));
            item.put("popularity", popularity);
        });
        map.put("merchantList", merchantEntityList);
        return R.success(map);
    }
}
